<?php
session_start();
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_booking.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_tickets.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_seats.php';


$payment_method = filter_input(INPUT_POST, "payment_method"); // need change to status of checkout

$book_info = $_SESSION['book_info'];
$coupon_id = 0;
if (!empty($book_info['coupon_id'])) {
    $coupon_id = $book_info['coupon_id'];
}

$booking_code = api_booking::finalize_booking(
    $book_info['booking_id'], 
    $book_info['user_id'], 
    $book_info['ticket_id'], 
    $book_info['ticket_quantity'], 
    $coupon_id, 
    $book_info['combo_id'], 
    $book_info['combo_quantity'],
    0);

if (!empty($payment_method) && $payment_method === "skrill") {
    $_SESSION['book_info']['status'] = "wait_skrill_payment";
    
} else if (!empty($payment_method) && $payment_method === "easypaisa") {
    $_SESSION['book_info']['status'] = "wait_easypaisa_payment";
    
    $total = api_booking::calculate_total_money(
            $_SESSION['book_info']['ticket_id'], 
            $_SESSION['book_info']['ticket_quantity'], 
            $_SESSION['book_info']['combo_id'], 
            $_SESSION['book_info']['combo_quantity'], 
            $_SESSION['book_info']['combo_id']);
    
    
    // send mail to user cinema
    $email_sent_to   = api_tickets::get_user_cinema_email($_SESSION['book_info']['ticket_id']);
    $message         = "A client (email: ".$_SESSION['user_info']['email'].") have booked. \n"
                     . "Booking ID: ".$book_info['booking_id']." \n"
                     . "This is booking code: ".$booking_code." \n"
                     . "Total client have to pay: " . $total . " USD \n"
                     . "After the money transfered, you need to change the status of booking to PAID.";
    mail($email_sent_to, "Client booking successfully and wait for user payment via Easypaisa.", $message);
    
    
    // send mail to client
    $email_sent_to   = $_SESSION['user_info']['email'];
    $message         = "This is your booking code: ".$booking_code." \n"
                     . "Please pay for the booking, and remember to write down your booking code when you transfer money. \n"
                     . "Total you have to pay: " . $total . " USD";
    mail($email_sent_to, "Congratulations, Booking successfully and need to payment via Easypaisa", $message);
}